<?php

/**
 * StaffTransfer form.
 *
 * @package    erp-dnict
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class StaffTransferForm extends BaseStaffTransferForm
{
  public function configure()
  {
  	$this->setWidgets(array(
        'old_gov_organization_id'       =>  new sfWidgetFormSelect(array('choices' => $this->getAllGovOrganizations())),
        'old_job_title'           		=>  new sfWidgetFormInput(),
        'new_gov_organization_id'       =>  new sfWidgetFormSelect(array('choices' => $this->getAllGovOrganizations())),
        'new_job_title'           		=>  new sfWidgetFormInput(),
        'start_date'                 	=>  new sfWidgetFormInput(),
        'document_id'                 	=>  new sfWidgetFormInput(),
        ));

  	$this->widgetSchema->setLabels(array(
  		'old_gov_organization_id'		=>	'Đơn vị cũ:',
  		'old_job_title'					=>	'Vị trí công tác cũ:',
  		'new_gov_organization_id'		=>	'Đơn vị mới:',
  		'new_job_title'					=>	'Vị trí công tác mới',
  		'start_date'					=>	'Ngày chuyển',
  		'document_id'					=>	'Quyết định số',
  		));

  	$this->setValidators(array(
  		'old_gov_organization_id'		=> new sfValidatorChoice(array('choices' => array_keys($this->getAllGovOrganizations()), 'required' => 'true')),
  		'new_gov_organization_id'		=> new sfValidatorChoice(array('choices' => array_keys($this->getAllGovOrganizations()), 'required' => 'true')),
  		'start_date'					=> new sfValidatorDate(array('required' => true, 'date_output' => 'dd/mm/yy', 'date_format' => "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/")),
  		'old_job_title'					=> new sfValidatorString(array('required' => true)),
  		'new_job_title'					=> new sfValidatorString(array('required' => true)),
  		'document_id'					=> new sfValidatorString(array('required' => true)),
  		));

  	$this->widgetSchema->setNameFormat('staff_transfer[%s]');
  }

  private function getAllGovOrganizations()
  {
    $items = array();
    $q = Doctrine_Query::create()
            ->from('GovOrganization go')
            ->where('go.is_active = ?', true);

    $results = $q->execute();
    foreach ($results as $result)
        $items[$result['id']] = $result['name'];

    return $items;
  }
}
